package com.heu.blood.storge.controller;

import com.heu.blood.common.aop.ControllerLogAnnotation;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.R;
import com.heu.blood.match.entity.BloodInformationEntity;
import com.heu.blood.storge.entity.BloodInStorgeEntity;
import com.heu.blood.storge.entity.BloodVerifyStorgeEntity;
import com.heu.blood.storge.service.BloodInStorgeService;
import com.heu.blood.storge.service.BloodVerifyStorgeService;
import com.heu.blood.storge.vo.BloodContactVerifyAndInStorgeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import static com.heu.blood.common.enums.BloodStorgeEnum.BLOOD_VERIFY_SUCCESS;


/**
 *
 *
 * @author lyj
 * @email lyj@heu.com
 * @date 2023-06-16 00:33:53
 */
@Slf4j
@RestController
@RequestMapping("storge/bloodverifystorge")
public class BloodVerifyStorgeController {
    @Autowired
    private BloodVerifyStorgeService bloodVerifyStorgeService;

    /**
     * 列表
     */
    @ControllerLogAnnotation(module = "血液验证", operate = "分页查询血液验证信息")
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        log.info("params : {}", params); // 打印参数
        PageUtils page = bloodVerifyStorgeService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @ControllerLogAnnotation(module = "血液验证", operate = "根据id查询血液验证信息")
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        BloodVerifyStorgeEntity bloodVerifyStorge = bloodVerifyStorgeService.getById(id);

        return R.ok().put("data", bloodVerifyStorge);
    }

    /**
     * 保存
     */
    @ControllerLogAnnotation(module = "血液验证", operate = "插入血液验证信息")
    @RequestMapping("/saveWithCheck")
    public R save(@RequestBody BloodVerifyStorgeEntity bloodVerifyStorge){
        int flag = bloodVerifyStorgeService.saveWithCheck(bloodVerifyStorge);
        return R.ok();
    }

    /**
     * 批量保存，同时更新状态为待核对
     */
    @ControllerLogAnnotation(module = "血液验证", operate = "批量插入血液验证信息")
     @RequestMapping("/batchsave")
     public R batchSave(@RequestBody List<BloodVerifyStorgeEntity> bloodVerifyStorgeEntityList){
         int flag = bloodVerifyStorgeService.batchSave(bloodVerifyStorgeEntityList);
         return R.ok();
     }
    /**
     * 修改
     */
    @ControllerLogAnnotation(module = "血液验证", operate = "更新血液验证信息")
    @RequestMapping("/update")
    public R update(@RequestBody BloodVerifyStorgeEntity bloodVerifyStorge){
        log.info(String.valueOf(bloodVerifyStorge.getId()));
        log.info(String.valueOf(bloodVerifyStorge));
        bloodVerifyStorgeService.updateById(bloodVerifyStorge);

        return R.ok();
    }

    /**
     * 删除
     */
    @ControllerLogAnnotation(module = "血液验证", operate = "根据id删除血液验证信息")
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
        bloodVerifyStorgeService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }
    //bloodinformation 接收的是扫码录出来的信息
    //单条检验
    //
    @ControllerLogAnnotation(module = "血液验证", operate = "初始的单条验证血液信息(传入的血液信息对象)")
    @RequestMapping("/bloodCheck")
    public R bloodCheck(@RequestBody BloodInformationEntity bloodInformationEntity){
        //如果bloodInformationEntity为空
        if(bloodInformationEntity == null){
            return R.ok("传输的对象为空");
        }
        //接收对象
        BloodVerifyStorgeEntity bloodVerifyStorgeEntityResult = bloodVerifyStorgeService.bloodCheck(bloodInformationEntity);
        log.info(String.valueOf(bloodVerifyStorgeEntityResult));
        //检验是否为空 为空则证明插入不成功
        if(bloodVerifyStorgeEntityResult == null){
            //没查到血液
            return R.ok("血液不存在,检验失败");
        }else if(bloodVerifyStorgeEntityResult.getVerifyResult().equals(BLOOD_VERIFY_SUCCESS.code())){
            //如果接收到通过 则检验成功
            //前端验证然后再进行入库
            return R.ok("血液检验成功");
        }else {
            //返回检验失败原因
            return R.ok(bloodVerifyStorgeEntityResult.getVerifyResult());

        }
    }

    @ControllerLogAnnotation(module = "血液验证", operate = "废弃的批量导入excel中的血液验证信息")
    @RequestMapping("/excelForVerifyStorge")
    public R exportForVerifyStorgeByExcel(@RequestParam("file") MultipartFile file) throws IOException {
        String fileName = file.getOriginalFilename(); // 获取文件名

        InputStream is = null;

        try{
            is = file.getInputStream();
            List<Map> List =bloodVerifyStorgeService.getListByExcel(is,fileName);// 获取解析后的List集合
            bloodVerifyStorgeService.BatchImportBloodVerifyStorge(List);

//            // System.out.println(studentList.toString());
            Boolean result = bloodVerifyStorgeService.BatchImportBloodVerifyStorge(List);; // 把数据插入数据库
            if (result){
                return R.ok("文件上传成功！");
            }else {
                return R.error( "文件上传失败！");
            }
        }catch (Exception e){
            e.printStackTrace();
           } finally {
               is.close();
           }
           return R.error("文件错误！");
       }

    @ControllerLogAnnotation(module = "血液验证", operate = "批量导入excel中的血液验证信息")
    @PostMapping("/uploadExcelForBloodVerify")
    public R importExcel(@RequestParam("file") MultipartFile multipartFile){
        System.out.println("--------------");
        System.out.println(multipartFile);
        bloodVerifyStorgeService.importExcelToBloodVerify(multipartFile);
        //bloodInformationService.importExcel(multipartFile);
        return R.ok("ok");
    }
    //批量检验
    @ControllerLogAnnotation(module = "血液验证", operate = "批量导入excel中的血液验证信息")
    @RequestMapping("/batchBloodCheckforExcel")
    public R batchBloodTest(){
        //查找未对比检验血液
        List<BloodVerifyStorgeEntity> bloodVerifyStorgeEntityList = bloodVerifyStorgeService.listForBloodVerifyStorage();
        //进行检验操作 并进行插入和删除操作
        bloodVerifyStorgeService.bloodBatchCheck(bloodVerifyStorgeEntityList);
        return R.ok();
    }
    //单条检验 前端选一条血液检验表对象进行血液核验
    @ControllerLogAnnotation(module = "血液验证", operate = "新的单条血液检验(血液验证对象)")
    @RequestMapping("/simpleBloodCheck")
    //需要传入一个血液验证表的对象 即前端选择一条数据进行验证
    public R simpleBloodTest(BloodVerifyStorgeEntity bloodVerifyStorgeEntity){
        bloodVerifyStorgeService.bloodSimpleCheck(bloodVerifyStorgeEntity);
        return R.ok();
    }
    @ControllerLogAnnotation(module = "血液验证", operate = "新的多条血液检验(血液验证对象)")
    @RequestMapping("/batchBloodCheckforhtml")
    public R batchBloodCheckForHtml(List<BloodVerifyStorgeEntity> bloodVerifyStorgeEntityList){
        bloodVerifyStorgeService.bloodBatchCheck(bloodVerifyStorgeEntityList);
        return R.ok();
    }

    /**
     * 传递blood_id和失败原因 更新血液验证表的原因
     * @param bloodId
     * @param verifyUnsuccessReason
     * @return
     */
    @ControllerLogAnnotation(module = "血液验证", operate = "传递血液Id和失败原因 更新血液验证表的原因")
    @RequestMapping("/updateVerifyResultByBloodIdAndVerifyUnsuccessReason")
    public R UpdateVerifyResultByBloodIdAndVerifyUnsuccessReason(@RequestParam("bloodId") String bloodId,@RequestParam("verifyUnsuccessReason") String verifyUnsuccessReason){
        bloodVerifyStorgeService.UpdateVerifyResultByBloodIdAndVerifyUnsuccessReason(bloodId,verifyUnsuccessReason);
        return R.ok();
    }

    /**
     * 根据血液id联表查询入库表和验证表
     * @param params
     * @return
     */
    @ControllerLogAnnotation(module = "血液验证", operate = "根据血液id联表查询入库表和验证表")
    @RequestMapping("/bloodContactVerifyAndInStorgeByBloodId")
    public R bloodContactVerifyAndInStorgeByBloodId(@RequestParam Map<String, Object> params){
        log.info("params : {}", params); // 打印参数
        PageUtils page =bloodVerifyStorgeService.bloodContactVerifyAndInStorgeByBloodId(params);
        return R.ok().put("page", page);
    }

    /**
     * 根据用户检验信息来对比检验结果和库存结果
     * @param bloodVerifyStorgeEntity
     * @return
     */
    @ControllerLogAnnotation(module = "血液验证", operate = "根据用户检验信息来对比检验结果和库存结果")
    @RequestMapping("/getVerifyCompareRes")
    public R getVerifyCompareRes(@RequestBody BloodVerifyStorgeEntity bloodVerifyStorgeEntity){
        log.info("params : {}", bloodVerifyStorgeEntity); // 打印参数
        BloodContactVerifyAndInStorgeVo res = bloodVerifyStorgeService.getVerifyCompareRes(bloodVerifyStorgeEntity);
        return R.ok().put("data", res);
    }
    @ControllerLogAnnotation(module = "血液验证", operate = "根据bloodId来判断该袋血液是否在入库表中并且状态为入库待检验")
    @GetMapping("/checkInStorageTestStatus")
    public R checkInStorageTestStatus(@RequestParam String bloodId) {
        BloodInStorgeEntity entity = bloodVerifyStorgeService.checkInStorageTestStatus(bloodId);
        //return R.ok().put("isInStorageTest", isInTest);
        if (entity != null) {
            // 查到了，返回实体或继续处理
            return R.ok().put("inStorageTestInfo", entity);
        } else {
            // 没查到
            return R.error(400, "无法进行入库检测!");
        }
    }


}
